<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <!--搜索开始-->
        <fieldset class="layui-elem-field layuimini-search">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="" id="form">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">登录名</label>
                            <div class="layui-input-inline">
                                <input type="text" name="loginName" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">昵称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="nickName" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                        <label class="layui-form-label">手机号码</label>
                        <div class="layui-input-inline">
                            <input type="text" name="phone" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                        <div class="layui-form-item">
                            <div class="layui-block" style="text-align: center;">
                                <button type="submit" class="layui-btn layui-icon-search" lay-submit
                                        lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索
                                </button>
                                <button type="submit" class="layui-btn layui-btn-warm" lay-submit
                                        onclick="$('#form')[0].reset()" lay-filter="data-search-btn"><i
                                        class="layui-icon"></i> 重 置
                                </button>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>
        <!--搜索结束-->

        <!--表格开始-->
        <table class="layui-hide" id="dataTable" lay-filter="dataTable"></table>
        <!--表格结束-->

        <script type="text/html" id="switchTypeTpl">
            <input type="checkbox" lay-skin="switch" disabled="disabled" lay-text="普通用户|超级管理员" {{ d.type == 1?'checked' :''}}>
        </script>
        <script type="text/html" id="switchLockTpl">
            <input type="checkbox" lay-skin="switch" disabled="disabled" lay-text="否|是" {{ d.locked == true?'checked' :''}}>
        </script>
        <!--工具bar 和 字段过滤excel导出一起-->
        <script type="text/html" id="operateToolBar">
            <a class="layui-btn layui-btn-sm" lay-event="add">添加</a>
        </script>
        <!--操作的工具bar-->
        <script type="text/html" id="operateTableBar">
            <a class="layui-btn layui-btn-xs layui-btn-warm data-count-delete" lay-event="update">修改</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
            <a class="layui-btn layui-btn-xs layui-btn-warm data-count-delete" lay-event="show">授权</a>
            <a class="layui-btn layui-btn-xs layui-btn-warm data-count-delete" lay-event="reset">密码重置</a>
        </script>

        <!--添加和修改的弹出层开始-->
        <div id="addOrUpdateDiv" style="display: none;padding: 0.3125rem">
            <form method="post" class="layui-form layui-form-pane" lay-filter="dataForm" id="dataForm">
                <input type="hidden" name="id"/>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">昵称</label>
                        <div class="layui-input-inline">
                            <input type="text" name="nickName" id="nickName" lay-verify="required" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">登录名</label>
                        <div class="layui-input-inline">
                            <input type="text" name="loginName" id="loginName" lay-verify="required" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">密码</label>
                        <div class="layui-input-inline">
                            <input type="text" name="password" lay-verify="required" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">手机号码</label>
                        <div class="layui-input-inline">
                            <input type="text" name="phone" lay-verify="required" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">邮箱地址</label>
                        <div class="layui-input-inline">
                            <input type="text" name="email" lay-verify="required" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">用户类型</label>
                        <div class="layui-input-inline">
                            <input type="checkbox" checked="" name="type" lay-skin="switch" lay-filter="switchTest" lay-text="普通用户|超级管理员">
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block" style="text-align: center;">
                        <a class="layui-btn " lay-submit="" lay-filter="doSubmit"><label
                                class="layui-icon layui-icon-release"></label>提交</a>
                        <a class="layui-btn layui-btn-warm" onclick="javascript:$('#dataForm')[0].reset()"
                           lay-filter="data-search-btn"><label class="layui-icon layui-icon-refresh"></label>重置</a>
                    </div>
                </div>
            </form>
        </div>
        <!--添加和修改的弹出层结束-->

        <!--选择菜单和权限的弹出层开始-->
        <div id="selectRoleDiv" style="display: none;padding: 0.3125rem;">
            <table class="layui-hide" id="roleTable" lay-filter="roleTable"></table>
        </div>
        <!--选择菜单和权限的弹出层结束-->
    </div>
</div>

<script src="../../lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
<script src="../../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="../../js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script src="../../lib/jq-module/jquery.particleground.min.js" charset="utf-8"></script>
<script src="../../lib/common/jquery.cookie.min.js" charset="utf-8"></script>
<script src="../../lib/common/api.js" charset="utf-8"></script>
<script src="../../lib/common/Convert_Pinyin.js" charset="utf-8"></script>

<script>
    layui.use(['form', 'table', 'layer', 'treeTable'], function () {
        const form = layui.form;
        const table = layui.table;
        const layer = layui.layer;
        const treeTable = layui.treeTable;

        const tableIns = table.render({
            elem: '#dataTable',
            url: userInfo,
            toolbar: "#operateToolBar",
            cellMinWidth: true,
            cols: [[
                {type: "numbers", fixed: "left"},
                {field: 'id', title: 'ID', align: "center",hide:true},
                {field: 'loginName', title: '登录名', align: "center"},
                {field: 'nickName', title: '昵称', align: "center"},
                {field: 'password', title: '密码', align: "center",hide:true},
                {field: 'phone', title: '手机号码', align: "center"},
                {field: 'email', title: '邮箱地址', align: "center"},
                {field: 'locked', title: '是否锁定', align: "center",width: 90,templet: '#switchLockTpl'},
                {field: 'type', title: '用户类型', align: "center",width: 90,templet: '#switchTypeTpl'},
                {title: '操作', templet: '#operateTableBar', fixed: "right", align: "center"}
            ]],
            page: true
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function () {
            //执行搜索重载
            table.reload('dataTable', {
                page: {
                    curr: 1
                },
                where: $("#form").serializeObject()
            });
            return false;
        });
        //监听用户姓名失去焦点的事件
        $("#nickName").on("blur",function(){
            var name=$("#nickName").val();
            var fullName = pinyin.getFullChars(name);
            $("#loginName").val(fullName.toLowerCase());
        })

        //监听表头的事件
        table.on('toolbar(dataTable)', function (obj) {
            switch (obj.event) {
                case 'add':
                    openAddNotice();
                    break;
            }
        });

        //监听行数据的事件
        table.on('tool(dataTable)', function (obj) {
            const data = obj.data; //获得当前行数据
            if (obj.event === 'delete') {
                layer.confirm('真的删除这条数据么', function (index) {
                    $.ajax({
                        url: userInfo + "/" + data.id,
                        type: 'delete',
                        success: function (res) {
                            layer.msg(res.msg);
                            obj.del();
                            layer.close(index);
                        },
                        error: function (res) {
                            alert("删除错误");
                            console.log("删除错误");
                            console.log(res);
                        }
                    });
                });
            } else if (obj.event === 'update') {
                openUpdateNotice(data);
            } else if (obj.event === 'show') {
                show(data);
            }else if (obj.event === 'reset') {
                reset(data);
            }
        });

        //打开添加公告的弹层
        var mainIndex;
        var url;  //动态的url
        function openAddNotice() {
            mainIndex = layer.open({
                type: 1,
                title: '添加',
                content: $("#addOrUpdateDiv"),
                area: ['65%', '60%'],
                success: function () {
                    //重置表单
                    $("#dataForm")[0].reset();
                }
            });
        }

        function openUpdateNotice(data) {
            console.log(data);
            mainIndex = layer.open({
                type: 1,
                title: '修改',
                content: $("#addOrUpdateDiv"),
                area: ['65%', '60%'],
                success: function (index) {
                    //装载数据表格
                    form.val("dataForm", data);
                }
            });
        }

        //提交数据
        form.on("submit(doSubmit)", function () {
            let params = $("#dataForm").serializeObject();
            if(params.type === "on") {
                params.type = "1";
            } else {
                params.type = "0";
            }
            $.ajax({
                url: userInfo,
                data: JSON.stringify(params),
                type: "Post",
                dataType: "json",
                contentType: "application/json;charset=utf-8",
                success: function (data) {
                    console.log(data);
                    if (data.code === 0) {
                        tableIns.reload();
                    } else {
                        layer.msg("保存失败");
                    }
                    layer.close(mainIndex);
                },
                error: function (data) {
                    layer.msg("保存错误" + data);

                }
            });
            return false;
        });


        function show(data_table) {
            let table_RolesIns;
            mainIndex=layer.open({
                type:1,
                title:'分配【'+data_table.nickName+'】用户和角色',
                content:$("#selectRoleDiv"),
                area:['90%','90%'],
                btn:['<span class="layui-icon layui-icon-ok">确认分配</span>'],
                btnAlign:'c',
                yes:function(){
                    var checkStatus = table.checkStatus('roleTable');
                    var m=checkStatus.data;
                    var uid=data_table.id;
                    const params = [];
                    $.each(m,function(x,item){
                        params.push({"uid": uid, "rid": item.id});
                    });
                    $.ajax({
                        url: userInfoAuthRole,
                        data: JSON.stringify(params),
                        type: "Post",
                        dataType: "json",
                        contentType: "application/json;charset=utf-8",
                        success: function (data) {
                            if (data.code === 0) {
                                tableIns.reload();
                                layer.msg(data.msg);
                            } else {
                                layer.msg("保存失败");
                            }
                            layer.close(mainIndex);
                        },
                        error: function (data) {
                            layer.msg("保存错误" + data);

                        }
                    });
                },
                success:function(index){
                    table_RolesIns=table.render({
                        elem: '#roleTable',
                        url: roleInfoNoPage+"?userId="+data_table.id,
                        toolbar:false,
                        cellMinWidth:true,
                        cols: [[
                            {type: "checkbox",  fixed: "left"},
                            {field: 'id', title: 'ID', align: "center"},
                            {field: 'name', title: '角色名称', align: "center"},
                            {field: 'remark', title: '备注', align: "center"}
                        ]]

                    });

                }
            });
        }

        function reset(data) {
            $.ajax({
                url: userInfoResetPwd,
                data: JSON.stringify(data),
                type: "Post",
                dataType: "json",
                contentType: "application/json;charset=utf-8",
                success: function (res) {
                    layer.msg(res.msg);
                },
                error: function (res) {
                    alert("重置错误");
                    console.log("重置错误");
                    console.log(res);
                }
            });
        }

    });
</script>
</body>
</html>